/**
 * 附件查询列表面板
 */
Ext.namespace("Ext.ux.sysfile");

Ext.ux.sysfile.FileQueryPanel = Ext.extend(Ext.Panel, {

	border: false,
	region: 'center',
	layout: 'border',

	formPanel: null,
    grid: null,
	
	initComponent:function(){
		Ext.ux.sysfile.FileQueryPanel.superclass.initComponent.call(this);
		
		//查询部分
		var searchForm = new Ext.FormPanel({
			region: 'north',
			title: '附件查询',
			labelAlign: 'right',
			height: 90,
			frame: true,
			plain: true,
			collapsible: true,
			border: true,
			margins: '1 1 2 1',
			items: [{
				layout: 'column',
				items:[{
					columnWidth: .33,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '值对象名称',
						name: 'entity',
						anchor: '95%'
					}]
				},{
					columnWidth: .33,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '值对象主键',
						name: 'entityId',
						anchor: '95%'
					}]	
				},{
					columnWidth: .33,
					layout: 'form',
					items:[{
						xtype: 'textfield',
						fieldLabel: '附件编号',
						name:'fileCode',
						anchor: '95%'
					}]
				}]
			},{
				layout: 'column',
				items:[{
					columnWidth: .33,
					layout: 'form',
					items: [{
						xtype: 'textfield',
						fieldLabel: '附件名称',
						name: 'fileName',
						anchor: '95%'
					}]
				},{
					columnWidth: .33,
					layout: 'form',
					items:[{
						xtype: 'sysCodeCombo',
						fieldLabel: '附件格式',
						hiddenName:'fileFormat',
						typeCode:'ABF_FILE_TYPE',
						anchor: '95%'
					}]
				},{
					columnWidth: .33,
					layout: 'hbox',
					defaults:{margins:'0 5 0 0'},
					items:[{
						xtype: 'button',
						text: '查询',
						width: 80,
						handler: this.search,
						scope: this
					},{
						xtype: 'button',
						text: '清空',
						width: 80,
						handler: this.clearSearchForm,
						scope: this
					}]
				}]
			}]
		});
		this.add(searchForm);
		this.formPanel = searchForm;
	
		
		// 列表部分
		var gridStructure = [{  
			name: 'sysFileId',
			hidden: true
		},{
			name: 'entity',  
			header: "值对象名称",
			sortable: true,
			width: 120
		},{  
			name: 'entityId',  
			header: "值对象主键",
			sortable: true,
			width: 100
		},{  
			name: 'fileCode',  
			header: "附件编号",
			sortable: true,
			width: 120
		},{  
			name: 'fileName',  
			header: "附件名称",
			sortable: true,
			width: 200
		},{  
			name: 'fileType',  
			header: "附件类型",
			sortable: true,
			width: 100
		},{  
			name: 'fileFormat',  
			header: "附件格式",
			sortable: true,
			width: 100
		},{  
			name: 'filePath',  
			header: "附件路径",
			sortable: true,
			width: 300
		},{  
			name: 'cmp',  
			header: "创建人",
			sortable: true,
			width: 100
		},{  
			name: 'cdt',  
			header: "创建时间",
			sortable: true,
			type: 'datetime',
			width: 150
		},{  
			name: 'eemp',  
			header: "修改人",
			sortable: true,
			width: 100
		},{  
			name: 'edt',  
			header: "修改时间",
			sortable: true,
			type: 'datetime',
			width: 150
		},{
			name: 'memo1',  
			header: "备注1",
			width: 150
		},{
			name: 'memo2',  
			header: "备注2",
			width: 150
		}];
	   
		var grid = new Ext.ux.grid.CrudGrid({
			title: '附件列表',
			region: 'center',
			margins: '0 1 1 1',
			structure: gridStructure,
			url: 'sys-file!list.action',
			defaultSortField: 'fileCode',  
			keyField: 'sysFileId'
		});
		
		grid.getTopToolbar().addItem('-',{
			text: '全文在线编辑',
			tooltip: '同时编辑附件记录信息及关联文档内容，仅适用于Office文档',
			iconCls: 'icon-edit',
			handler: this.editFormAndDoc,
			scope: this
		});
		
		grid.getTopToolbar().addItem('-',{
			text: '在线编辑文档',
			tooltip: '在线编辑文档内容，仅适用于Office文档',
			iconCls: 'icon-edit',
			handler: this.editOfficeDoc,
			scope: this
		});

		grid.on('rowdblclick', function(grid, rowIndex, e){
			var sel = grid.getSelectionModel().getSelections();
			var sysFileId = sel[0].get('sysFileId');
			this.loadById(sysFileId);
		}, this);
	
		grid.on('addRecord', function(){
			var win = new Ext.ux.sysfile.FileDetailWindow({title:'新增附件'});
			win.show();
		});
		
		grid.on('updateRecord', function(sysFileId){
			this.loadById(sysFileId);
		}, this);
		
		grid.on('deleteRecord', function(grid, ids){
			Ext.Ajax.request({
				url: 'sys-file!delete.action',
				success: function(responese){
					this.search();
					Ext.Msg.alert('系统提示', '已成功删除选中的附件记录！');
				},
				failure: function(){
					Ext.Msg.alert('系统提示', '删除失败');
				},
				method: 'POST',
				params:{
					sysFileIds: ids
				},
				scope: this
			});
		}, this);
	
		this.add(grid);
		this.grid = grid;
	
		grid.store.on('beforeload', function(){
			grid.store.baseParams = {
				entity: this.formPanel.form.findField('entity').getValue(),
				entityId: this.formPanel.form.findField('entityId').getValue(),
				fileCode: this.formPanel.form.findField('fileCode').getValue(),
				fileName: this.formPanel.form.findField('fileName').getValue(),
				fileFormat: this.formPanel.form.findField('fileFormat').getValue()
			};
		}, this);
	},
	
	//查询
	search: function(){
		this.grid.store.reload();
	},
	
	//清空查询条件
	clearSearchForm: function(){
		this.formPanel.form.reset();
		this.formPanel.form.findField('entity').focus();
		this.search();
	},
	
	//加载附件信息
	loadById: function(id){
		var win = new Ext.ux.sysfile.FileDetailWindow({title:'修改附件'});
		win.loadById(id);
	},
	
	//全文在线编辑（同时编辑附件记录及文档内容）
	editFormAndDoc: function(){
		var sysFileId = this.getOfficeFileId();
		if(sysFileId != null)
			editFormAndDoc(sysFileId);
	},
	
	//在线编辑文档内容
	editOfficeDoc: function(){
		var sysFileId = this.getOfficeFileId();
		if(sysFileId != null)
			editOfficeDoc(sysFileId);
	},
	
	//获取Office文档格式的附件记录ID
	getOfficeFileId: function(){
		var sel = this.grid.getSelectionModel().getSelections();
		if(sel.length == 1){
			var id = sel[0].get('sysFileId');
			var fileName = sel[0].get('fileName');

			if(isNtkoSupportFormat(getFileFormat(fileName))){
				return id;
			}else{
				Ext.Msg.alert('系统提示', '选择的附件记录必须是NTKO控件所支持的Office文档格式，请重新选择！');
				return null;
			}
		}else{
			Ext.Msg.alert('系统提示', '请选择一条要在线编辑的记录');
			return null;
		}
	}
});